const sessions = {
  state: {
    usingTrafficChannel: []
  },
  mutations: {
    setUsingTrafficChannel (state, data) {
      state.usingTrafficChannel = data
    },
    pushUsingTrafficChannel (state, data) {
      let exist = state.usingTrafficChannel.find(item => item.caller_cu_device === data.caller_cu_device && item.called_cu === data.called_cu)
      if (exist) return
      state.usingTrafficChannel.push(data)
    },
    modifyUsingSubscribeFreqSegment (state, data) {
      for (let key in state.usingTrafficChannel) {
        if (state.usingTrafficChannel[key].caller_cu_device === data.caller_cu_device && state.usingTrafficChannel[key].called_cu === data.called_cu) {
          state.usingTrafficChannel.splice(key, 1)
          state.usingTrafficChannel.push(data)
        }
      }
    },
    deleteUsingTrafficChannel (state, data) {
      for (let i in state.usingTrafficChannel) {
        if (state.usingTrafficChannel[i].caller_cu_device === data.caller_cu_device && state.usingTrafficChannel[i].called_cu === data.called_cu) {
          state.usingTrafficChannel.splice(i, 1)
        }
      }
    }
  },
  actions: {
    async getUsingTrafficChannel (context) {
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/abs/devices/sessions/'
      })
      context.commit('setUsingTrafficChannel', response.data)
      // 地图画线
      // for (let item of response.data) {
      //   setImmediate(() => {
      //     context.commit('pushStationMapLine', item)
      //   })
      // }
    },
    initOpenlayersMapLine (context) {
      for (let item of context.state.usingTrafficChannel) {
        setImmediate(() => {
          context.commit('pushStationMapLine', item)
        })
      }
    }
  },
  getters: {
    getTafficFreqSegmentChartData: (state) => {
      let res = []
      state.usingTrafficChannel.forEach(item => {
        res.push({
          satellite: item.caller_satellite_id,
          satellite_name: item.caller_satellite_name,
          type: 'traffic',
          down_freq_begin: item.caller_down_freq_begin / 1000,
          down_freq_end: item.caller_down_freq_end / 1000,
          power: item.caller_power,
          bandWidth: (item.caller_down_freq_end - item.caller_down_freq_begin) / 1000,
          centerFreq: (item.caller_down_freq_begin + item.caller_down_freq_end) / 2000,
          color: 'rgba(142, 255, 18, 1)'
        })

        res.push({
          satellite: item.called_satellite_id,
          satellite_name: item.called_satellite_name,
          type: 'traffic',
          down_freq_begin: item.called_down_freq_begin / 1000,
          down_freq_end: item.called_down_freq_end / 1000,
          power: item.called_power,
          bandWidth: (item.called_down_freq_end - item.called_down_freq_begin) / 1000,
          centerFreq: (item.called_down_freq_begin + item.called_down_freq_end) / 2000,
          color: 'rgba(142, 255, 18, 1)'
        })
      })
      return res
    },
    getMapLineData: (state) => {
      let res = []
      state.usingTrafficChannel.forEach(item => {
        res.push({
          name: ``,
          coords: [
            [item.caller_station_longitude, item.caller_station_latitude],
            [item.called_station_longitude, item.called_station_latitude]
          ]
        })
      })
      return res
    }
  }
}
export default sessions
